package generator.service.impl;

import com.atkt.ktkt.model.activity.CouponInfo;
import com.atkt.ktkt.model.activity.CouponUse;
import com.atkt.ktkt.model.user.UserInfo;
import com.atkt.ktkt.vo.activity.CouponUseQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktkt.client.user.UserInfoFeignClient;
import generator.service.CouponInfoService;
import generator.mapper.CouponInfoMapper;
import generator.service.CouponUseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
* @author 罗雅文
* @description 针对表【coupon_info(优惠券信息)】的数据库操作Service实现
* @createDate 2022-09-24 17:15:07
*/
@Service
public class CouponInfoServiceImpl extends ServiceImpl<CouponInfoMapper, CouponInfo>
    implements CouponInfoService{

    @Autowired
    private CouponUseService couponUseService;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    //获取已经使用优惠卷列表(条件查询分页)
    @Override
    public Page<CouponUse> selectCouponUsePage(Page<CouponUse> couponUsePage, CouponUseQueryVo couponUseQueryVo) {
        //获取条件
        Long couponId = couponUseQueryVo.getCouponId();
        String couponStatus = couponUseQueryVo.getCouponStatus();
        String getTimeBegin = couponUseQueryVo.getGetTimeBegin();
        String getTimeEnd = couponUseQueryVo.getGetTimeEnd();
        //封装条件
        QueryWrapper<CouponUse> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(couponId)) {
            wrapper.eq("coupon_id",couponId);
        }
        if(!StringUtils.isEmpty(couponStatus)) {
            wrapper.eq("coupon_status",couponStatus);
        }
        if(!StringUtils.isEmpty(getTimeBegin)) {
            wrapper.ge("get_time",getTimeBegin);
        }
        if(!StringUtils.isEmpty(getTimeEnd)) {
            wrapper.le("get_time",getTimeEnd);
        }
        //调用方法进行条件分页查询
        Page<CouponUse> pageModel = couponUseService.page(couponUsePage, wrapper);
        List<CouponUse> records = pageModel.getRecords();
        records.stream().forEach(item->{
            this.getUserInfoById(item);
        });
        return pageModel;
    }

    @Override
    public void updateCouponInfoUseStatus(Long couponUseId, Long orderId) {
        CouponUse couponUse = new CouponUse();
        couponUse.setId(couponUseId);
        couponUse.setOrderId(orderId);
        couponUse.setCouponStatus("1");
        couponUse.setUsingTime(new Date());
        couponUseService.updateById(couponUse);
    }

    //根据用户id 通过远程调用得到用户信息
    private CouponUse getUserInfoById(CouponUse item) {
        //获取用户id
        Long userId = item.getUserId();
        if (!StringUtils.isEmpty(userId)){
            UserInfo userInfo = userInfoFeignClient.getById(userId);
            if (userInfo!=null){
                item.getParam().put("nickName",userInfo.getNickName());
                item.getParam().put("phone",userInfo.getPhone());
            }
        }
        return item;
    }
}




